<?php
/**
 * @Author: rocky
 * Date: 2019/10/8
 * Time: 13:55
 */


namespace app\shop\controller\api\v1;


use app\api\service\WechatMiniService;
use app\common\service\WechatMini;
use app\shop\model\ShopOrder;

class Notify
{
    /*
     * 购买商品微信支付回调
     */
    public function pay()
    {
        logger('notify', 'pay', '====pay return====');
        $data = WechatMini::WePayOrder()->getNotify();
        logger('notify', $data, '====pay return====');
        if ($data['return_code'] == 'SUCCESS') {
            
            //查询订单号，通过订单号获取订单类型
            $order = model('ShopOrder')->where('order_no',$data['out_trade_no'])->find();
            if ($order['type'] !=1) {
                //付款后成为会员,更新user数据表is_vip字段
                $User=model('User')->where('openid',$data['openid'])->find();
                if($User){
                    $User->is_vip = 1;//lid=1是充值套餐
                    $User->save();
                }else{
                    $this->errorCode(100, '充值写入数据失败！');
                }
            }
                
            $res = ShopOrder::pay(1, $data['out_trade_no'], $data['cash_fee'] / 100, $data['transaction_id']);
            if ($res) {
                return WechatMini::WePayOrder()->getNotifySuccessReply();
            }
        }
    }
}